﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HotelManagement._04.OBJECT;
using HotelManagement._02.BUS;
using HotelManagement;

namespace HotelManagement._01.FORM
{
    public partial class F32_RoomType : Form
    {
        RoomInfo m_RoomInfo;
        RoomInfoBUS m_RoomInfoBUS;
        public F32_RoomType()
        {
            InitializeComponent();
            Init();
            LoadData();
            if (AssetsGS.instance.RoleUser == "User")
            {
                m_btn_Insert.Enabled = false;
                m_btn_Update.Enabled = false;
            }
        }
        public void Init()
        {
            m_RoomInfo = new RoomInfo();
            m_RoomInfoBUS = new RoomInfoBUS();
        }

        public void LoadData()
        {
            m_dgv_RoomInfo.DataSource = m_RoomInfoBUS.ViewRoomType();
        }

        private void m_dgv_RoomInfo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                m_RoomInfo.RoomTypeID = int.Parse(m_dgv_RoomInfo.Rows[m_dgv_RoomInfo.CurrentCell.RowIndex].Cells["RoomTypeID"].Value.ToString());
                m_RoomInfo.RoomTypeDescription = m_dgv_RoomInfo.Rows[m_dgv_RoomInfo.CurrentCell.RowIndex].Cells["RoomTypeDescription"].Value.ToString();
                m_RoomInfo.NumberOfBed = m_dgv_RoomInfo.Rows[m_dgv_RoomInfo.CurrentCell.RowIndex].Cells["NumberOfBed"].Value.ToString();
                m_RoomInfo.Cost = int.Parse(m_dgv_RoomInfo.Rows[m_dgv_RoomInfo.CurrentCell.RowIndex].Cells["Cost"].Value.ToString());

                m_txt_Description.Text = m_RoomInfo.RoomTypeDescription;
                m_txt_NumberOfBed.Text = m_RoomInfo.NumberOfBed;
                m_txt_Cost.Text = m_RoomInfo.Cost.ToString();
                m_btn_Insert.Enabled = false;
            }
            catch
            {

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void m_btn_Update_Click(object sender, EventArgs e)
        {
            if (m_RoomInfo.RoomTypeID == 0)
            {
                MessageBox.Show("Bạn chưa chọn phòng nào!");
            }
            else
            {
                DialogResult OK;
                OK = MessageBox.Show("Bạn muốn thay đổi loại phòng đã chọn ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (OK == DialogResult.Yes)
                {
                    m_RoomInfo.NumberOfBed = m_txt_NumberOfBed.Text.ToString();
                    m_RoomInfo.RoomTypeDescription = m_txt_Description.Text.ToString();
                    if (!CValidate.IsNumber(m_txt_Cost.Text.ToString().Trim()))
                    {
                        MessageBox.Show("Lỗi định dạng tiền !");
                        return;
                    }
                    m_RoomInfo.Cost = int.Parse(m_txt_Cost.Text.ToString().Trim());
                    if (m_RoomInfoBUS.UpdateRoomType(m_RoomInfo))
                    {
                        MessageBox.Show("Thành công !");
                        LoadData();
                    }
                    else MessageBox.Show("Có lỗi! Không cập nhật được.");
                    m_btn_Insert.Enabled = true;
                }
            }

        }

        private void m_btn_Insert_Click(object sender, EventArgs e)
        {
            if (m_txt_Description.Text=="" || m_txt_NumberOfBed.Text==""||m_txt_Cost.Text=="")
            {
                MessageBox.Show("Bạn chưa nhập đủ dữ liệu!");
            }
            else
            {
                DialogResult OK;
                OK = MessageBox.Show("Bạn muốn thêm loại phòng mới ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (OK == DialogResult.Yes)
                {
                    m_RoomInfo.NumberOfBed = m_txt_NumberOfBed.Text.ToString();
                    m_RoomInfo.RoomTypeDescription = m_txt_Description.Text.ToString();
                    if (!CValidate.IsNumber(m_txt_Cost.Text.ToString().Trim()))
                    {
                        MessageBox.Show("Lỗi định dạng tiền !");
                        return;
                    } 
                    m_RoomInfo.Cost = int.Parse(m_txt_Cost.Text.ToString().Trim());
                    if (m_RoomInfoBUS.InsertRoomType(m_RoomInfo))
                    {
                        MessageBox.Show("Thành công !");
                        LoadData();
                    }
                    else MessageBox.Show("Có lỗi! Không thêm được.");
                }
            }
        }

    }
}
